package hdgeom.hEdge;



import java.util.Collections;
import java.util.Vector;
import processing.core.PVector;

public class HENode extends PVector{
	public int id=-1;
	
	//public HEdge hEdge;
	
	public Object data;
	public boolean fix;
	public boolean border;
	public boolean checked;
	
	Vector<HEdge>hEdges=new Vector<HEdge>();
	public void emptyHEdges(){
		hEdges.removeAllElements();
	}
	public void setPosition(float x,float y,float z){
		this.set(x,y,z);
	}
	public void setPosition(float x,float y){
		this.set(x,y);
	}
	public void connect(){
		for (int i=0;i<hEdges.size();i++){
			HEdge hE1=hEdges.get(i);
			HEdge hE2=hEdges.get((i+1)%hEdges.size());
			//hE1.next=hE2.getSym();
			hE2.getSym().setNext(hE1);
		}
	}
	public void detachHE(HEdge hEdge){
		hEdge.setNext(hEdge.getSym());
		hEdges.remove(hEdge);
		connect();
	}
	
	public void attachHE(HEdge hEdge){
		hEdges.add(hEdge);
		Collections.sort(hEdges,new HEdgeAngleComparator());
		connect();
	}
	public float dist2D(float x,float y){
		return dist2D(this.x,this.y,x,y);
	}
	public static float dist2D(float x1,float y1,float x2,float y2){
		float dX=x1-x2;
		float dY=y1-y2;
		return (float)Math.sqrt(dX*dX+dY*dY);
	}
	public HENode() {
		super();
		// TODO Auto-generated constructor stub
	}
	public HENode(float x, float y, float z) {
		super(x, y, z);
		// TODO Auto-generated constructor stub
	}
	public HENode(PVector p) {
		super(p.x, p.y, p.z);
		// TODO Auto-generated constructor stub
	}
	public HENode(float x, float y) {
		super(x, y);
		// TODO Auto-generated constructor stub
	}
	
	
	public boolean isNb(HENode n2){
		for (int i=0;i<hEdges.size();i++){
			HEdge hEdge=hEdges.get(i);
			if (hEdge.getDest()==(n2)){
				return true;
			}
		}
		return false;
		/*HEdge nHEdge = hEdge;
		if (nHEdge==null)return false;
		do {
			if (nHEdge.getDest().equalPosition(n2)){
				return true;
			}
			nHEdge = nHEdge.getNextO();
		} while (!nHEdge.equals(hEdge));
		return false;*/
	}
	
	public HEdge getHEdge(HENode n2){
		for (int i=0;i<hEdges.size();i++){
			HEdge hEdge=hEdges.get(i);
			if (hEdge.getDest()==n2){
				return hEdge;
			}
		}
		return null;
	}

	public Vector<HENode>getNbs(){
		Vector<HENode>nbs=new Vector<HENode>();
		for (int i=0;i<hEdges.size();i++){
			HEdge hEdge=hEdges.get(i);
			nbs.add(hEdge.getDest());
			
		}
		/*HEdge nHEdge=hEdge;
		if (hEdge==null)return nbs;
		do{
			HENode nb=nHEdge.getDest();
			nbs.add(nb);
			nHEdge = nHEdge.getNextO();
		}
		while(!nHEdge.equals(hEdge));*/
		return nbs;
	}
	public int getNHEdges(){
		return hEdges.size();
		/*int n=0;
		HEdge nHEdge=hEdge;
		if (hEdge==null)return 0;
		do{
			n++;
			nHEdge = nHEdge.getNextO();
		}
		while(nHEdge!=hEdge);
		return n;*/
	}
	public Vector<HEdge> getHEdges(){
		return hEdges;
		/*Vector<HEdge>edges=new Vector<HEdge>();
		HEdge nHEdge=hEdge;
		if (hEdge==null)return edges;
		do{
			edges.add(nHEdge);
			nHEdge = nHEdge.getNextO();
		}
		while(!nHEdge.equals(hEdge));
		return edges;*/
	}
	public HEdge getHEdge() {
		if (hEdges.size()==0)return null;
		return hEdges.firstElement();
		//return hEdge;
	}

	public void setHEdge(HEdge edge) {
		//hEdge = edge;
		
	}

	
	public Object getData() {
		// TODO Auto-generated method stub
		return data;
	}
	public void setData(Object data) {
		// TODO Auto-generated method stub
		this.data=data;
	}
	public boolean checkSequence(){
		if (hEdges.size()<=2)return true;
		for (int i=0;i<hEdges.size();i++){
			HEdge h=hEdges.get(i);
			HEdge hPrev=h.getPrevO();
			HEdge hNext=h.getNextO();
			double a1=hPrev.getCCWAngleBetween(h);
			double a2=hPrev.getCCWAngleBetween(hNext);
			if (a2>a1)return false;
		}
		return true;
	}
	
	public boolean isChecked() {
		return checked;
	}
	public void setChecked(boolean checked) {
		this.checked = checked;
	}
	
	

}
